from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy

from config import APPCONFIG

# data base，1_创建数据库
db = SQLAlchemy()

def create_app(config_name: str):
    #blueprint_5.导入创建的蓝图
    from app.views import index_blp

    app = Flask(__name__)

    app.config.from_object(APPCONFIG.get(config_name))

    #blueprint_6.将蓝图注册到app上
    #url_prefix用于给前台和后台相同的路由名称添加一个区分，即后台所有路由添加前缀
    #app.register_blueprint(admin_blp, url_prefix="/admin")
    app.register_blueprint(index_blp)

    # data base，2_初始化数据库
    db.init_app(app)

    # 创建数据库
    with app.app_context():
        db.create_all()

    @app.errorhandler(404)
    def page_error(e):
        return render_template("404_page.html"), 404

    return app
